package com.cyl.manager.lbs.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.cyl.manager.act.domain.entity.CouponCategory;
import com.cyl.manager.act.service.CouponCategoryService;
import com.cyl.manager.lbs.convert.LbsProductConvert;
import com.cyl.manager.lbs.domain.LbsProduct;
import com.cyl.manager.lbs.pojo.query.LbsAddProductQuery;
import com.cyl.manager.lbs.pojo.query.LbsProductQuery;
import com.cyl.manager.lbs.service.LbsProductService;
import com.cyl.manager.pms.domain.entity.Product;
import com.cyl.manager.pms.mapper.ProductMapper;
import com.cyl.manager.lbs.pojo.vo.LbsProductVO;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 直播商品Controller
 *
 * @author hxw
 * @date 2025-02-09
 */
@Api(description ="直播商品接口列表")
@RestController
@RequestMapping("/lbs/lbsProduct")
public class LbsProductController extends BaseController {
    @Autowired
    private LbsProductService service;
    @Autowired
    private LbsProductConvert convert;

    @Autowired
    private ProductMapper productMapper;
    

    @Autowired
    private CouponCategoryService couponCategoryService;
    @ApiOperation("查询直播商品列表")
    @PreAuthorize("@ss.hasPermi('lbs:lbsProduct:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<LbsProduct>> list(@RequestBody LbsProductQuery query, Pageable page) {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<LbsProduct> list = service.selectList(query, page);
        for (LbsProduct lbsProduct : list) {
             Map<String, Object> params = new HashMap<String, Object>();
             Product product = productMapper.selectById(lbsProduct.getPmsProductId());
             params.put("product", product);
             if(product!=null && product.getCplx()!=null && product.getDhCouponCategoryId()!=null) {
              	CouponCategory couponCategory = couponCategoryService.selectCheckById(product.getDhCouponCategoryId());
              	if(couponCategory!=null) {
              		params.put("flqmc", couponCategory.getTitle());
              	}
              }
        	lbsProduct.setParams(params);
		}
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }
 

    @ApiOperation("获取直播商品详细信息")
    @PreAuthorize("@ss.hasPermi('lbs:lbsProduct:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<LbsProduct> getInfo(@PathVariable("id") Long id) {
    	LbsProduct lbsProduct = service.selectById(id);
    	Map<String, Object> params = new HashMap<String, Object>();
        Product product = productMapper.selectById(lbsProduct.getPmsProductId());
        params.put("product", product);
        if(product!=null && product.getCplx()!=null && product.getDhCouponCategoryId()!=null) {
         	CouponCategory couponCategory = couponCategoryService.selectCheckById(product.getDhCouponCategoryId());
         	if(couponCategory!=null) {
         		params.put("flqmc", couponCategory.getTitle());
         	}
         }
    	lbsProduct.setParams(params);
        return ResponseEntity.ok(lbsProduct);
    }

    @ApiOperation("新增直播商品")
    @PreAuthorize("@ss.hasPermi('lbs:lbsProduct:add')")
    @Log(title = "直播商品", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody LbsAddProductQuery lbsAddProductQuery) {
    	service.insert(lbsAddProductQuery);
        return ResponseEntity.ok(1);
    }

    @ApiOperation("讲解")
    @PreAuthorize("@ss.hasPermi('lbs:lbsProduct:edit')")
    @Log(title = "讲解", businessType = BusinessType.UPDATE)
    @PutMapping("/activityTopProduct")
    public ResponseEntity<Integer> activityTopProduct(@RequestBody LbsProduct lbsProduct) {
    	service.activityTopProduct(lbsProduct);
        return ResponseEntity.ok(1);
    }
    

    
//
//    @ApiOperation("删除直播商品")
//    @PreAuthorize("@ss.hasPermi('lbs:lbsProduct:remove')")
//    @Log(title = "直播商品", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
//        return ResponseEntity.ok(service.deleteByIds(ids));
//    }
}
